package com.itheima.reggie.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.OrdersDemo;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired(required = false)
    private OrdersService ordersService;
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @PostMapping("/submit")
    public R<Orders> submit(@RequestBody Orders orders, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        orders.setUserId(userId);
        ordersService.submit(orders);
        return R.success(orders);
    }
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page,int pageSize){
        Page<OrdersDto> ordersPage=ordersService.userPage(page,pageSize);
        return R.success(ordersPage);
    }
    @GetMapping("/page")
    private R<Page<OrdersDto>> page(int page, int pageSize, String number, String beginTime,String endTime){
        Page<OrdersDto> ordersDtoPage=ordersService.page(page,pageSize,number,beginTime,endTime);
        return R.success(ordersDtoPage);
    }
//
    @GetMapping("download")

    public void download(HttpServletResponse response) throws IOException {

        List<Orders> ordersList=ordersMapper.find();
        List<OrdersDemo> collect = ordersList.stream().map(orders -> {
            OrdersDemo ordersDemo = new OrdersDemo();
            ordersDemo.setNumber(orders.getNumber());
            ordersDemo.setStatus(orders.getStatus());
            ordersDemo.setUserName(orders.getUserName());
            ordersDemo.setPhone(orders.getPhone());
            ordersDemo.setAddress(orders.getAddress());
            ordersDemo.setOrderTime(orders.getOrderTime().toString());
            ordersDemo.setAmount(orders.getAmount());
            return ordersDemo;
        }).collect(Collectors.toList());
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),OrdersDemo.class).sheet("模板").doWrite(collect);

    }
}
